#include "addfrienddialog.h"

//////////////////////////////////////
/// 表示一个好友搜索的结果
//////////////////////////////////////
FriendResultItem::FriendResultItem(const model::UserInfo &userInfo) : _userInfo(userInfo)
{
    // 1.设置基本属性
    this->setFixedHeight(70);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 2. 创建垂直布局管理器
    QGridLayout *layout = new QGridLayout();
    layout->setVerticalSpacing(0);
    layout->setHorizontalSpacing(10);
    layout->setContentsMargins(0, 0, 20, 0);
    this->setLayout(layout);

    // 3. 创建头像
    QPushButton *avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(50, 50);
    avatarBtn->setIconSize(QSize(50, 50));
    avatarBtn->setIcon(userInfo.avatar);

    // 4. 创建昵称
    // 整个 item 的高度为 70，昵称和个性签名各占一半
    QLabel *nameLabel = new QLabel();
    nameLabel->setFixedHeight(35);
    nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    nameLabel->setStyleSheet("QLabel { font-size: 16px; font-weight: 700;}");
    nameLabel->setText(userInfo.nickname);

    // 5. 创建个性签名
    QLabel *descLabel = new QLabel();
    descLabel->setFixedHeight(35);
    descLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    descLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    descLabel->setStyleSheet("QLabel { font-size: 14px; }");
    descLabel->setText(userInfo.description);

    // 6. 创建添加好友按钮
    _addBtn = new QPushButton();
    _addBtn->setFixedSize(100, 40);
    _addBtn->setText("添加好友");
    QString btnStyle = "QPushButton { border: none; background-color: rgb(137, 217, 97); color: rgb(255, 255, 255); "
                       "border-radius: 10px;} ";
    btnStyle += "QPushButton:pressed { background-color: rgb(200, 200, 200); }";
    _addBtn->setStyleSheet(btnStyle);

    // 7. 将控件添加到布局管理器中
    layout->addWidget(avatarBtn, 0, 0, 2, 1);
    layout->addWidget(nameLabel, 0, 1);
    layout->addWidget(descLabel, 1, 1);
    layout->addWidget(_addBtn, 0, 2, 2, 1);
}

//////////////////////////////////////
/// 整个搜索好友的窗口
//////////////////////////////////////
AddFriendDialog::AddFriendDialog(QWidget *parent)
{
    // 1. 设置基本属性
    this->setFixedSize(500, 500);
    this->setWindowTitle("添加好友");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QDialog {background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 添加布局管理器
    _layout = new QGridLayout();
    _layout->setSpacing(10);
    _layout->setContentsMargins(20, 20, 20, 0);
    this->setLayout(_layout);

    // 3. 创建搜索框
    _searchEdit = new QLineEdit();
    _searchEdit->setFixedHeight(50);
    _searchEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    QString style = "QLineEdit { border: none; border-radius: 10px; font-size: 16px; background-color: rgb(240, 240, "
                    "240); padding-left: 5px;}";
    _searchEdit->setPlaceholderText("按手机号/用户序号/昵称搜索");
    _layout->addWidget(_searchEdit, 0, 0, 1, 8);

    // 4. 创建搜索按钮
    QPushButton *searchBtn = new QPushButton();
    searchBtn->setFixedSize(50, 50);
    searchBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    searchBtn->setIcon(QIcon(":/resource/image/search.png"));
    QString btnStyle = "QPushButton { border: none; background-color: rgb(240, 240, 240); border-radius: 10px; }";
    btnStyle += "QPushButton:hover { background-color: rgb(220, 220, 220); } QPushButton:pressed { background-color: "
                "rgb(200, 200, 200); } ";
    searchBtn->setStyleSheet(btnStyle);
    _layout->addWidget(searchBtn, 0, 8, 1, 1);

    // 5. 添加滚动区域
    initResultArea();
}

void AddFriendDialog::initResultArea()
{
    // 1. 创建滚动区域对象
    QScrollArea *scrollArea = new QScrollArea();
    scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    scrollArea->setWidgetResizable(true);
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal {height: 0;} ");
    scrollArea->verticalScrollBar()->setStyleSheet(
        "QScrollBar:vertical {width: 2px; background-color: rgb(255, 255, 255);}");
    scrollArea->setStyleSheet("QScrollArea { border: none; }");
    _layout->addWidget(scrollArea, 1, 0, 1, 9);

    // 2. 创建 QWidget
    _resultContainer = new QWidget();
    _resultContainer->setObjectName("_resultContainer");
    _resultContainer->setStyleSheet("#_resultContainer { background-color: rgb(255, 255, 255); }");
    scrollArea->setWidget(_resultContainer);

    // 3. 给这个 QWidget 添加元素需要创建垂直布局管理器
    QVBoxLayout *vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0, 0, 0, 0);
    _resultContainer->setLayout(vlayout);
}

void AddFriendDialog::clear()
{
    QVBoxLayout *layout = dynamic_cast<QVBoxLayout *>(_resultContainer->layout());
    for (int i = layout->count() - 1; i >= 0; i--)
    {
        QLayoutItem *layoutItem = layout->takeAt(i);
        if (layoutItem == nullptr || layoutItem->widget() == nullptr)
        {
            continue;
        }
        delete layoutItem->widget();
    }
}

void AddFriendDialog::setSearchKey(const QString &searchKey) { _searchEdit->setText(searchKey); }
